import { utils, writeFile } from "xlsx"
import { ArticleItem } from "@/types/admin/article"
// 导出为excel数据
export default function downExcel(data: ArticleItem[], filename = "excel") {
  console.log(data, filename)
  let xlsxData = [["标题", "分类ID", "分类名称", "作者", "封面", "描述", "标签", "发布状况", "发布时间", "点赞数", "浏览量", "评论数"]] as any
  const list = data.map((item) => {
    return [
      item.title,
      item.categoryID,
      item.aboutInfo.category.name,
      item.aboutInfo?.user,
      item.cover,
      item.desc,
      item.tags?.map((i: any) => i.name).join(" "),
      item.isPublish ? "已发布" : "未发布",
      item.createTime,
      item.likeNum,
      item.viewNum,
      item.commentNum
    ]
  })
  xlsxData = xlsxData.concat(list)

  //   // 将数组转换为工作表
  const worksheet = utils.aoa_to_sheet(xlsxData)

  // 创建一个新的工作簿（Workbook），并将工作表添加进去
  const workbook = utils.book_new()
  utils.book_append_sheet(workbook, worksheet, "Sheet1")

  // 将工作簿写入到Excel文件中
  writeFile(workbook, `${filename}.xlsx`)
}
